package com.example.teaching_server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 学生信息数据传输对象
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "学生详细信息")
public class StudentDTO {

    @NotNull(message = "学生ID不能为空")
    @Positive(message = "学生ID必须是正整数")
    @Schema(description = "学生唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer id;

    @NotBlank(message = "用户名不能为空")
    @Schema(description = "用户名", example = "zhangsan", requiredMode = Schema.RequiredMode.REQUIRED)
    private String username;

    @NotBlank(message = "学生姓名不能为空")
    @Schema(description = "学生姓名", example = "张三", requiredMode = Schema.RequiredMode.REQUIRED)
    private String name;

    @NotBlank(message = "角色不能为空")
    @Schema(description = "用户角色", example = "student", requiredMode = Schema.RequiredMode.REQUIRED)
    private String role;

    @NotBlank(message = "学号不能为空")
    @Schema(description = "学号", example = "2024001", requiredMode = Schema.RequiredMode.REQUIRED)
    private String studentId;

    @NotBlank(message = "年级不能为空")
    @Schema(description = "年级", example = "一年级", requiredMode = Schema.RequiredMode.REQUIRED)
    private String gradeLevel;

    @NotBlank(message = "班级名称不能为空")
    @Schema(description = "班级名称", example = "一年级一班", requiredMode = Schema.RequiredMode.REQUIRED)
    private String className;

    @Schema(description = "平均成绩", example = "85.5")
    private Double averageScore;

    @Min(value = 0, message = "出勤率不能小于0")
    @Max(value = 100, message = "出勤率不能大于100")
    @Schema(description = "出勤率", example = "95.0")
    private Double attendanceRate;

    @Schema(description = "荣誉评价", example = "excellent", allowableValues = {"excellent", "good", "average", "poor"})
    private String honorEvaluation;

    @Schema(description = "联系电话")
    private String phone;

    @Schema(description = "邮箱")
    private String email;

    @Schema(description = "头像URL")
    private String avatar;

    @Schema(description = "入学日期", example = "2024-09-01")
    private String enrollmentDate;

    /**
     * 将统计评价转换为中文标签
     */
    @Schema(description = "荣誉评价中文标签", accessMode = Schema.AccessMode.READ_ONLY)
    public String getHonorLabel() {
        return switch (honorEvaluation) {
            case "excellent" -> "优秀";
            case "good" -> "良好";
            case "average" -> "一般";
            case "poor" -> "待改进";
            default -> honorEvaluation != null ? honorEvaluation : "未评价";
        };
    }

    /**
     * 获取完整学生信息
     */
    @Schema(description = "完整学生信息", accessMode = Schema.AccessMode.READ_ONLY)
    public String getFullInfo() {
        return name + " (" + studentId + ") - " + className;
    }

    /**
     * 判断是否为优秀学生
     */
    @Schema(description = "是否为优秀学生", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isExcellentStudent() {
        return "excellent".equals(honorEvaluation);
    }

    /**
     * 获取成绩等级
     */
    @Schema(description = "成绩等级", accessMode = Schema.AccessMode.READ_ONLY)
    public String getScoreGrade() {
        if (averageScore == null) return "未评分";
        if (averageScore >= 90) return "优秀";
        if (averageScore >= 80) return "良好";
        if (averageScore >= 70) return "中等";
        if (averageScore >= 60) return "及格";
        return "不及格";
    }

    /**
     * 构建基础学生信息 - 手动实现Builder方法
     */
    public static StudentDTO basic(Integer id, String name, String studentId, String className) {
        StudentDTO dto = new StudentDTO();
        dto.setId(id);
        dto.setName(name);
        dto.setStudentId(studentId);
        dto.setClassName(className);
        dto.setRole("student");
        return dto;
    }

    /**
     * 手动实现Builder类
     */
    public static class StudentDTOBuilder {
        private Integer id;
        private String username;
        private String name;
        private String role;
        private String studentId;
        private String gradeLevel;
        private String className;
        private Double averageScore;
        private Double attendanceRate;
        private String honorEvaluation;
        private String phone;
        private String email;
        private String avatar;
        private String enrollmentDate;

        public StudentDTOBuilder id(Integer id) {
            this.id = id;
            return this;
        }

        public StudentDTOBuilder username(String username) {
            this.username = username;
            return this;
        }

        public StudentDTOBuilder name(String name) {
            this.name = name;
            return this;
        }

        public StudentDTOBuilder role(String role) {
            this.role = role;
            return this;
        }

        public StudentDTOBuilder studentId(String studentId) {
            this.studentId = studentId;
            return this;
        }

        public StudentDTOBuilder gradeLevel(String gradeLevel) {
            this.gradeLevel = gradeLevel;
            return this;
        }

        public StudentDTOBuilder className(String className) {
            this.className = className;
            return this;
        }

        public StudentDTOBuilder averageScore(Double averageScore) {
            this.averageScore = averageScore;
            return this;
        }

        public StudentDTOBuilder attendanceRate(Double attendanceRate) {
            this.attendanceRate = attendanceRate;
            return this;
        }

        public StudentDTOBuilder honorEvaluation(String honorEvaluation) {
            this.honorEvaluation = honorEvaluation;
            return this;
        }

        public StudentDTOBuilder phone(String phone) {
            this.phone = phone;
            return this;
        }

        public StudentDTOBuilder email(String email) {
            this.email = email;
            return this;
        }

        public StudentDTOBuilder avatar(String avatar) {
            this.avatar = avatar;
            return this;
        }

        public StudentDTOBuilder enrollmentDate(String enrollmentDate) {
            this.enrollmentDate = enrollmentDate;
            return this;
        }

        public StudentDTO build() {
            StudentDTO studentDTO = new StudentDTO();
            studentDTO.setId(this.id);
            studentDTO.setUsername(this.username);
            studentDTO.setName(this.name);
            studentDTO.setRole(this.role);
            studentDTO.setStudentId(this.studentId);
            studentDTO.setGradeLevel(this.gradeLevel);
            studentDTO.setClassName(this.className);
            studentDTO.setAverageScore(this.averageScore);
            studentDTO.setAttendanceRate(this.attendanceRate);
            studentDTO.setHonorEvaluation(this.honorEvaluation);
            studentDTO.setPhone(this.phone);
            studentDTO.setEmail(this.email);
            studentDTO.setAvatar(this.avatar);
            studentDTO.setEnrollmentDate(this.enrollmentDate);
            return studentDTO;
        }
    }
}
